﻿/**--
指令使用方法说明：

<drop-down jisu-model="sysRole"></drop-down>
**/
var dropdownApp = angular.module("dropdownApp", ["Common", "720kb.tooltips"]);
dropdownApp.directive("dropDownTag", ["Conne_knowl", "sysUserInfo", function (RTS, sysUserInfo) {
    return {
        scope: {
            newjson: "=tagObj",
            previewitem: "=",
            previewplay: "&"
        },
        templateUrl: "/system/directive/drop-down/drop-down-tag.html",
        transclude: true,
        replace: false,
        restrict: 'AE',
        link: function ($scope, el, attr) {
            $scope.lableshowflag = false;
            // 数据源
            if ($scope.newjson != undefined) {
                $scope.yuanjson = $scope.newjson.oldtag;
                $scope.yuanjson.push($scope.newjson.newtag[0]);
                // 获取个人标签名称
                $scope.userparam = { userid: sysUserInfo.getuserinfo().user_ID, orgid: sysUserInfo.getuserinfo().organization_ID };
                RTS.one("Kapi").customPOST({ name: "My Message" }, "gettaglist", $scope.userparam).then(function (tagdata) {
                    //            RTS.gettaglist($scope.userparam).then(function (tagdata) {
                    if (tagdata.errorcode == 0) {
                        //                    console.log(tagdata.datas);
                        //系统标签和用户标签组合
                        if (tagdata.datas.length > 0) {
                            angular.forEach($scope.yuanjson, function (taginfo, index) {
                                for (var i = 0; i < tagdata.datas.length; i++) {
                                    if (tagdata.datas[i].tagid == $scope.yuanjson[index].tagid) {
                                        $scope.yuanjson.splice(index, 1, tagdata.datas[i]);
                                    }
                                }
                            });
                        }
                    } else {
                        console.log("directive---标签加载失败！");
                    }
                });


                $scope.lableshow = function () {
                    $scope.lableshowflag = !$scope.lableshowflag;
                }

                // 添加标签
                $scope.addnewobj = function (yuanitem) {
                    if ($scope.newjson.newtag.length == 0) {
                        $scope.newjson.newtag.push(yuanitem);
                    } else {
                        $scope.newjson.newtag[0] = yuanitem;
                    }
                }
            }
        },
        controller: function ($scope, $document) {
            // 预览
            $scope.isplay = function (item) {
                $scope.upobj.isbtn = true;
                //console.log(item.resdata); //传输完成后的数据 filepreview                      
                $scope.previewitem = item;
                //$scope.openipplay();
            }
            $scope.openipplay = function () {// 此方法传值报空控制器只能用监听
                $scope.previewplay();
            }
            //点击非指定区域，关闭显示层
            $document.bind("click", function (evt) {
                var evt = window.event ? window.event : evt,
                target = evt.srcElement || evt.target;
                if ($document[0].getElementById("knowledgewjsxkkbiaoqianbody") != null) {
                    var bt = $document[0].getElementById("knowledgewjsxkkbiaoqianbody").contains(target);
                    if (!bt) {
                        $scope.$apply(function () {
                            $scope.lableshowflag = false;
                        })
                    }
                }
            });
        }
    }
} ])

// 上传文件便签选择
dropdownApp.directive("dropDownUpLable", ["Conne_knowl", "sysUserInfo", function (RTS, sysUserInfo) {
    return {
        scope: {
            newjson: "=tagObj"
        },
        templateUrl: "/system/directive/drop-down/drop-down-up-lable.html",
        transclude: true,
        replace: false,
        restrict: 'AE',
        link: function ($scope, el, attr) {
            $scope.lableshowflag = false;
            // 数据源
            //            $scope.yuanjson = $scope.newjson.oldtag;

            // 获取个人标签名称
            $scope.userparam = { userid: sysUserInfo.getuserinfo().user_ID, orgid: sysUserInfo.getuserinfo().organization_ID };
            RTS.one("Kapi").customPOST({ name: "My Message" }, "gettaglist", $scope.userparam).then(function (tagdata) {
                //RTS.gettaglist($scope.userparam).then(function (tagdata) {
                if (tagdata.errorcode == 0) {
                    //                    console.log(tagdata.datas);
                    //系统标签和用户标签组合
                    if (tagdata.datas.length > 0) {
                        angular.forEach($scope.newjson.oldtag, function (taginfo, index) {
                            for (var i = 0; i < tagdata.datas.length; i++) {
                                if (tagdata.datas[i].tagid == $scope.newjson.oldtag[index].tagid) {
                                    $scope.newjson.oldtag.splice(index, 1, tagdata.datas[i]);
                                }
                            }
                        });
                    }
                } else {
                    console.log("directive---标签加载失败！");
                }
            });

            //$scope.newjson = [];    // 选中的json
            $scope.lableshow = function () {
                $scope.lableshowflag = !$scope.lableshowflag;
            }

            // 获取删除的索引 
            $scope.getindex = function (item, list) {
                var inde = -1;
                if (list.length > 0) {
                    list.forEach(function (data, index, array) {
                        if (array[index].tagid == item.tagid) {
                            inde = index;
                        }
                    });
                }
                return inde;
            }

            // 添加标签
            $scope.addnewobj = function (yuanitem) {
                $scope.newjson.newtag.push(yuanitem);
                //                console.log($scope.getindex(yuanitem, $scope.yuanjson));
                $scope.newjson.oldtag.splice($scope.getindex(yuanitem, $scope.newjson.oldtag), 1);
                //                console.info($scope.yuanjson, $scope.newjson.newtag);
            }

            //删除
            $scope.addyuanobj = function (newitem) {
                $scope.newjson.oldtag.push(newitem);
                $scope.newjson.newtag.splice($scope.getindex(newitem, $scope.newjson.newtag), 1);
                //                console.info($scope.yuanjson, $scope.newjson.newtag);
            }

            //            console.log($scope.newjson.newtag);

            //            //剔除已经选择的下拉项
            //            if ($scope.newjson.newtag.length > 0) {
            //                for (var i = 0; i < $scope.newjson.newtag.length; i++) {
            //                    $scope.yuanjson.splice($scope.getindex($scope.newjson.newtag[i], $scope.yuanjson), 1);
            //                }
            //            }

        },
        controller: function ($scope, $document) {
            //点击非指定区域，关闭显示层
            $document.bind("click", function (evt) {
                var evt = window.event ? window.event : evt,
                target = evt.srcElement || evt.target;
                if ($document[0].getElementById("dropdownuplable") != null) {
                    var bt = $document[0].getElementById("dropdownuplable").contains(target);
                    if (!bt) {
                        $scope.$apply(function () {
                            $scope.lableshowflag = false;
                        })
                    }
                }
            });
        }
    }
} ])
// 上传文件使用 
//      {
//                upflag:true|false，   //显示隐藏
//                isbtn:true,               // 是否是通过按扭传过来的   
//                datas:[{},{},{}]
//       }
dropdownApp.directive("dropDownUpFile", ["$timeout", function ($timeout) {
    return {
        scope: {
            upobj: "=upObj"
        },
        templateUrl: "/system/directive/drop-down/drop-down-up-file.html",
        transclude: true,
        replace: false,
        restrict: 'AE',
        link: function ($scope, el, attr) {


            var timeo;  //创建一个时间
            //鼠标悬浮显示下拉框
            /*
            $scope.showupflagfun = function () {
                $scope.$apply(function () {
                    $scope.upobj.upflag = true;
                });
            }

            $scope.hideupflagfun = function () {
                $scope.$apply(function () {
                    $scope.upobj.upflag = false;
                });
            }
            */
            el.bind("mouseenter", function () {                
                timeo = $timeout(function () {
                    $scope.$apply(function () {
                        $scope.upobj.upflag = true;
                    });
                }, 300);
            });

            el.bind("mouseleave", function () {                
                $timeout.cancel(timeo);
                $scope.$apply(function () {
                    $scope.upobj.upflag = false;
                });
            });
            /*
            $scope.hideupflagfun = function () {//点击查看传输列表
                $scope.upobj.upflag = false; // 标志按扭点击的
            }
            */
            // 删除问及爱女
            $scope.isremove = function (item) {
                $scope.upobj.isbtn = true;
                item.remove();
            }
            // 预览
            $scope.isplay = function (item) {
                $scope.upobj.isbtn = true;
                console.log(item.resdata); //传输完成后的数据 filepreview
                window.open("http://www.baidu.com");
            }
        },
        controller: function ($scope, $document) {
            //点击非指定区域，关闭显示层
            $document.bind("click", function (evt) {
                var evt = window.event ? window.event : evt,
                target = evt.srcElement || evt.target;
                if ($document[0].getElementById("knowledgeright-up") != null) {
                    if (!$scope.upobj.isbtn) {   // 如果是按扭返回回来的那么会走else
                        var bt = $document[0].getElementById("knowledgeright-up").contains(target);
                        if (!bt) {
                            $scope.$apply(function () {
                                $scope.upobj.upflag = false;
                            })
                        }
                    } else {
                        $scope.upobj.isbtn = false;
                    }
                }
                //if (!$scope.upobj.upflag) { // 当为false 时取消事件绑定
                //    $document.unbind("click");
                //}
            });
        }

    }
} ])
//人员下拉列表
dropdownApp.directive("dropDown", ["$http", "sysUserInfo", "$timeout", function ($http, sysUserInfo, $timeout) {
    return {
        scope: {
            ddObj: "= jisuModel"
        },
        templateUrl: "/system/directive/drop-down/drop-down.html",
        transclude: true,
        replace: false,
        restrict: 'AE',
        link: function ($scope, elm, attr) {
            var timeo;  //创建一个时间
            //鼠标悬浮显示下拉框
            /*
            $scope.showobjinfo = function () {
                //                timeo = $timeout(function () {
                $scope.$apply(function () {
                    $scope.ddObj.showflag = true;
                });
                //                }, 1000);
            }

            $scope.hideobjinfo = function () {
                //                $timeout.cancel(timeo);
                $scope.$apply(function () {
                    $scope.ddObj.showflag = false;
                });
            }
            */

            elm.bind("mouseenter", function () {                
                timeo = $timeout(function () {
                    $scope.$apply(function () {
                        $scope.ddObj.showflag = true;
                    });
                }, 300);
            });

            elm.bind("mouseleave", function () {                
                $timeout.cancel(timeo);
                $scope.$apply(function () {
                    $scope.ddObj.showflag = false;
                });
            });

            // optype1、用户状态   0正常 1锁定
            // optype2、系统角色  1超级管理员、2普通管理员、3教师、99学员
            $scope.updoptype = function (stat_role, optype) {
                if ($scope.ddObj.showflag) { $scope.ddObj.showflag = false; } else { $scope.ddObj.showflag = true; }
                if (optype == "2") {
                    if (stat_role != "2") {
                        $scope.ddObj.state = stat_role;
                    }
                } else if (optype == "1") {
                    if (stat_role != "1") {
                        $scope.ddObj.state = stat_role;
                    }
                }
            }

            //排序
            $scope.updopsort = function (stat_role, optype) {
                if ($scope.ddObj.orderby == "desc") {
                    $scope.ddObj.ddicon = "&#xec4f;";
                    $scope.ddObj.orderby = "asc";
                } else if ($scope.ddObj.orderby == "asc") {
                    $scope.ddObj.ddicon = "&#xe6b1;";
                    $scope.ddObj.orderby = "desc";
                }
            }

            //教师登录影藏“管理员”选项
            $scope.getOptype = function (optype) {
                return !(sysUserInfo.getuserinfo().powerLV == 3 && optype == 2);
            }
        },
        controller: function ($scope, $document) {
            //点击非指定区域，关闭显示层
            $document.bind("click", function (evt) {
                var evt = window.event ? window.event : evt,
                target = evt.srcElement || evt.target;
                if ($document[0].getElementById($scope.ddObj.divclass + "") != null) {
                    var bt = $document[0].getElementById($scope.ddObj.divclass + "").contains(target);
                    if (!bt) {
                        $scope.$apply(function () {
                            $scope.ddObj.showflag = false;
                        })
                    }
                }
            });
        }
    };
} ]);
//鼠标悬浮提示框
dropdownApp.directive("mousTitle", ["$timeout", function ($timeout) {
    return {
        scope: {
            ddObj: "=",
            showmsg: "&",
            hidemsg: "&"
        },
        //transclude: true,
        replace: false,
        restrict: 'AE',
        link: function ($scope, elm, attr) {            
            var timeo;  //创建一个时间
            //鼠标悬浮显示下拉框
            elm.bind("mouseenter", function () {                
                timeo = $timeout(function () {
                    $scope.$apply(function () {
                        $scope.ddObj = true;
                        $scope.showmsg();
                    });
                }, 300);
            });

            elm.bind("mouseleave", function () {                
                $timeout.cancel(timeo);
                $scope.$apply(function () {
                    $scope.ddObj = false;
                    $scope.hidemsg();
                });
            });

        }
    };
} ]);